<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/Reports' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card" style="margin-top: 20px">
      <el-button type="primary" style="margin-bottom: 20px" @click="addShow"
        >添加分类</el-button
      >
      <!-- min.js里引入应用 -->
      <tree-table
        :data="classifyList"
        :columns="columns"
        index-text="索引"
        show-index
        :selection-type="false"
        :expand-type="false"
      >
        <!-- 是否有效 -->
        <template slot="result" slot-scope="scope">
          <i
            :class="scope.row.cat_deleted ? 'el-icon-error' : 'el-icon-success'"
            :style="scope.row.cat_deleted ? 'color:red' : 'color:#90ee90'"
          ></i>
        </template>
        <!-- 排序 -->
        <template slot-scope="scope" slot="order">
          <el-tag v-if="scope.row.cat_level == 0">一级</el-tag>
          <el-tag v-if="scope.row.cat_level == 1" type="success">二级</el-tag>
          <el-tag v-if="scope.row.cat_level == 2" type="warning">三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot-scope="scope" slot="operate">
          <el-button type="primary" size="mini" @click="edit(scope.row)">
            编辑
          </el-button>
          <el-button type="danger" size="mini" @click="del(scope.row.cat_id)">
            删除
          </el-button>
        </template></tree-table
      >
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="info.pagenum"
        :page-sizes="[1, 3, 5, 7]"
        :page-size="info.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        style="margin-top: 20px"
      >
      </el-pagination>
      <!-- 添加的模态框 -->
      <el-dialog title="提示" :visible.sync="dialogVisible" width="50%">
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="分类名称" prop="cat_name">
            <el-input v-model="ruleForm.cat_name"></el-input>
          </el-form-item>
          <el-form-item label="父级分类">
            <el-cascader
              v-model="kes"
              :options="options"
              :props="props"
              style="width: 100%"
            ></el-cascader>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addClass">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 编辑的模态框 -->
      <el-dialog title="编辑分类" :visible.sync="dialogVisible1" width="50%">
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="分类名称" prop="cat_name">
            <el-input v-model="ruleForm.cat_name"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editAdd">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "MyVueCategories",

  data() {
    return {
      // 商品分类的数据
      classifyList: [],
      // 商品列表的参数
      info: {
        pagenum: 1,
        pagesize: 5,
      },
      columns: [
        {
          label: "分类名称",
          prop: "cat_name",
        },
        {
          label: "是否有效",
          prop: "cat_deleted",
          type: "template",
          template: "result",
        },
        {
          label: "排序",
          prop: "cat_level",
          type: "template",
          template: "order",
        },
        {
          label: "操作",
          type: "template",
          template: "operate",
        },
      ],
      //   总分条数
      total: 0,
      //   添加模态框的标识
      dialogVisible: false,
      //   编辑模态框的标识
      dialogVisible1: false,
      ruleForm: {
        cat_name: "",
        cat_pid: "",
        cat_level: "",
      },
      //   正则校验
      rules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
          { min: 1, max: 8, message: "长度在 1 到 8 个字符", trigger: "blur" },
        ],
      },
      //配置选项
      props: {
        expandTrigger: "hover",
        label: "cat_name",
        value: "cat_id",
        children: "children",
        checkStrictly: true,
      },
      kes: "",
      //   可选的数据源
      options: [],
      //   分类id
      catId: "",
    };
  },

  mounted() {
    this.list();
  },

  methods: {
    // 渲染
    list() {
      this.$http.get(`categories`, { params: this.info }).then((res) => {
        this.classifyList = res.data.data.result;
        this.total = res.data.data.total;
      });
    },
    //   分页
    handleSizeChange(val) {
      this.info.pagesize = val;
      this.list();
    },
    handleCurrentChange(val) {
      this.info.pagenum = val;
      this.list();
    },
    // 添加
    addShow() {
      this.dialogVisible = true;
      this.ruleForm = {};
      this.kes = "";
      this.$http.get(`categories`, { params: { type: 2 } }).then((res) => {
   
        this.options = res.data.data;
      });
    },
    // 添加分类
    addClass() {
      this.$refs.ruleForm.validate((valid) => {
        if (!valid) {
          this.$message("输入信息不正确");
        } else {
          // 判断获取到的数组长度   kes数组里边放的时下标
          if (this.kes.length == 0) {
            this.ruleForm.cat_pid = 0;
            this.ruleForm.cat_level = 0;
          } else {
            this.ruleForm.cat_pid = this.kes[this.kes.length - 1];
            this.ruleForm.cat_level = this.kes.length;
          }
          this.$http.post(`categories`, this.ruleForm).then((res) => {
            if (res.data.meta.status == 201) {
              this.$message.success(res.data.meta.msg);
              this.list();
              this.dialogVisible = false;
            } else {
              this.$message.error(res.data.meta.msg);
            }
          });
        }
      });
    },
    // 编辑
    edit(row) {
      this.dialogVisible1 = true;
      this.catId = row.cat_id;
      console.log(this.catId);
      this.ruleForm.cat_name = row.cat_name;
    },
    // 编辑确定
    editAdd() {
      this.$refs.ruleForm.validate((valid) => {
        if (!valid) {
          this.$message("输入信息不正确");
        } else {
          this.$http
            .put(`categories/${this.catId}`, this.ruleForm)
            .then((res) => {
              if (res.data.meta.status == 200) {
                this.$message.success(res.data.meta.msg);
                this.list();
                this.dialogVisible1 = false;
              } else {
                this.$message.error(res.data.meta, msg);
              }
            });
        }
      });
    },
    // 删除
    del(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http.delete(`categories/${id}`).then((res) => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
     this.list()
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style lang="scss" scoped>
</style>